package com.lanou.demo.backgrounddemo.controller;

import com.lanou.demo.backgrounddemo.service.ContentCategoryService;
import com.lanou.entity.entity.ContentCategoryEntity;
import com.lanou.entity.entity.TreeEntity;
import com.lanou.entity.utils.AjaxResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by 常宏盛
 * Date: 2019/7/12 14:41
 **/
@RestController
@RequestMapping("/content/category")
public class ContentCategoryController {

    @Resource
    private ContentCategoryService contentCategoryService;

    /**
     * 内容分类整理
     *
     * @param parentId
     * @return
     */
    @RequestMapping("/list")
    public List<TreeEntity> list(@RequestParam(name = "id", defaultValue = "0") Long parentId) {
        return this.contentCategoryService.list(parentId);
    }

    /**
     * 内容分类添加
     *
     * @param contentCategory
     * @return
     */
    @RequestMapping("/create")
    public AjaxResult create(ContentCategoryEntity contentCategory) {
        int createResult = this.contentCategoryService.create(contentCategory);
        if (createResult == 0) {
            return AjaxResult.builder().status(400).build();
        }
        return AjaxResult.builder().data(contentCategory.getId()).status(200).build();
    }

    /**
     * 内容分类重命名
     *
     * @param id
     * @param name
     */
    @RequestMapping("/update")
    public void update(Long id, String name) {
        this.contentCategoryService.update(id, name);
    }

    /**
     * 内容分类删除
     *
     * @param id
     */
    @RequestMapping("/delete")
    public void delete(Long id) {
        this.contentCategoryService.delete(id);
    }

}
